package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.*;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {

    void insert(Orders orders);
    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);
    //查看历史订单

    Page<OrdersDTO> getHistoryOrders(Long userId,Integer status);
    //临时解决方式
    @Select("select *from orders where user_id=#{currentId}")
    List< OrdersDTO>  select(Long currentId);
    //根据ordersId查order_detail
    @Select("select *from order_detail where order_id=#{id}")
    List<OrderDetail> getOrderDetails(Long id);
    //根据id查询订单
    @Select("select *from orders where id=#{id}")
    Orders getOrders(Long id);
    //订单搜索
    Page<OrderVO> search(OrdersPageQueryDTO dto);
    //统计待派送数量
    @Select("select count(*) from orders where status=#{confirmed}")
    Integer statisticsCONFIRMED(Integer confirmed);
    //统计派送中数量
    @Select("select count(*) from orders where status=#{deliveryInProgress}")
    Integer statisticsDELIVERY_IN_PROGRESS(Integer deliveryInProgress);
    //统计待接单数量
    @Select("select count(*) from orders where status=#{toBeConfirmed}")
    Integer statisticsTO_BE_CONFIRMED(Integer toBeConfirmed);
    //接单
    @Update("update orders set status=#{status} where id=#{id}")
    void confirm(OrdersConfirmDTO dto);
    // 拒单
    @Update("update orders set status=#{status},rejection_reason=#{dto.rejectionReason} where id=#{dto.id}")
    void rejection(OrdersRejectionDTO dto,Integer status);
    //取消订单
    @Update("update orders set status=#{status},cancel_reason=#{dto.cancelReason},cancel_time=#{cancelTime} where id=#{dto.id}")
    void cancle(OrdersCancelDTO dto, Integer  status, LocalDateTime cancelTime);
    //配送订单
    @Update("update orders set status=#{status},estimated_delivery_time=#{estimatedDeliveryTime} where id=#{id}")
    void delivery(Orders orders);
    //完成配送
    @Update("update orders set status=#{status},delivery_time=#{deliveryTime} where id=#{id}")
    void complete(Orders orders);
    //根据状态和下单时间查询订单
    @Select("select *from orders where status=#{pendingPayment} and order_time<#{time}")
    List<Orders> selectByStatusAndOrderTime(Integer pendingPayment, LocalDateTime time);
    //统计每日营业额
    @Select("select sum(amount) from orders where status=#{status} and order_time between #{beginTime} and #{endTime}")
    Double sumByMap(Map map);
    //用户订单查询
    Integer countByMap(Map map);


    //<!-- top10销量统计-->
     List<Map> sumTop10(Map map);
}
